<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Gq\VideoMonitoring\Repository;

use Plugin\Gq\VideoMonitoring\Model\GqVideoCamera;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 摄像头管理 Repository类
 */
class GqVideoCameraRepository extends IRepository
{
   public function __construct(
        protected readonly GqVideoCamera $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 摄像头名称
        if (isset($params['name']) && !empty($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 摄像头简称
        if (isset($params['short_name']) && !empty($params['short_name'])) {
            $query->where('short_name', 'like', '%'.$params['short_name'].'%');
        }

        // 设备编号
        if (isset($params['device_code']) && !empty($params['device_code'])) {
            $query->where('device_code', 'like', '%'.$params['device_code'].'%');
        }

        // 设备序列号
        if (isset($params['device_serial']) && !empty($params['device_serial'])) {
            $query->where('device_serial', 'like', '%'.$params['device_serial'].'%');
        }

        // 厂家
        if (isset($params['manufacturer']) && !empty($params['manufacturer'])) {
            $query->where('manufacturer', 'like', '%'.$params['manufacturer'].'%');
        }

        // IP地址
        if (isset($params['ip_address']) && !empty($params['ip_address'])) {
            $query->where('ip_address', 'like', '%'.$params['ip_address'].'%');
        }

        // 端口号
        if (isset($params['port']) && !empty($params['port'])) {
            $query->where('port', '=', $params['port']);
        }

        // 账户
        if (isset($params['username']) && !empty($params['username'])) {
            $query->where('username', 'like', '%'.$params['username'].'%');
        }

        // 密码
        if (isset($params['password']) && !empty($params['password'])) {
            $query->where('password', 'like', '%'.$params['password'].'%');
        }

        // 播放地址(RTSP/FLV)
        if (isset($params['play_url']) && !empty($params['play_url'])) {
            $query->where('play_url', '=', $params['play_url']);
        }

        // 位置
        if (isset($params['location']) && !empty($params['location'])) {
            $query->where('location', 'like', '%'.$params['location'].'%');
        }

        // 经度坐标
        if (isset($params['longitude']) && !empty($params['longitude'])) {
            $query->where('longitude', '=', $params['longitude']);
        }

        // 纬度坐标
        if (isset($params['latitude']) && !empty($params['latitude'])) {
            $query->where('latitude', '=', $params['latitude']);
        }

        // 状态
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        if (isset($params['type']) && !empty($params['type'])) {
            $query->where('type', $params['type']);
        }

        return $query;
    }
}